package com.qj.platform.pay.core.service;

import com.qj.base.commons.exception.CodeDefinition;
import com.qj.base.commons.utils.SqlUtil;
import com.qj.platform.pay.commons.execption.SdsException;
import com.qj.platform.pay.core.dao.UserDao;
import com.qj.platform.pay.core.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by hezhijie on 2016/11/17.
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public List<User> queryList(String id,String userCode,String name,String machineCode,int pageNum,int pageSize){
        return userDao.queryList(id,userCode, name,machineCode, pageNum, pageSize);
    }

    public User login(String userCode,String pwd,String ip,String mac,String machineCode){
        User userByMachineCode=getUserByMachineCode(machineCode);
        if(userByMachineCode!=null&&!userCode.equals(userByMachineCode.getUserCode())){
            throw new SdsException(CodeDefinition.ILLEGAL_ARGUMENT_VALUE,"该机器已被其他用户占用。");
        }
        User user=getUserByCode(userCode);
        if(user==null){
            throw new SdsException(CodeDefinition.ILLEGAL_ARGUMENT_VALUE,"用户不存在。");
        }
        if(userByMachineCode==null) {
            userDao.insertUserMachine(userCode, machineCode);
        }
        return user;
    }

    public String getUserInfo(){
        return "";
    }



    public User getUserByCode(String userCode){
        List<User> userList=queryList(null,userCode,null,null, SqlUtil.FIRST_PAGE_NUM,1);
        if(userList!=null&&userList.size()>0){
            return userList.get(0);
        }
        return null;
    }

    public User getUserByMachineCode(String machineCode){
        List<User> userList=queryList(null,null,null,machineCode, SqlUtil.FIRST_PAGE_NUM,1);
        if(userList!=null&&userList.size()>0){
            return userList.get(0);
        }
        return null;
    }
}
